package com.kingdee.eas.minghua.contract.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.framework.DynamicObjectFactory;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.minghua.contract.BcxyDocFactory;
import com.kingdee.eas.minghua.contract.BcxyDocInfo;
import com.kingdee.eas.minghua.contract.LwfbhtDocFactory;
import com.kingdee.eas.minghua.contract.LwfbhtDocInfo;
import com.kingdee.eas.minghua.contract.QthtDocInfo;
import com.kingdee.eas.minghua.contract.QthtDocFactory;
import com.kingdee.eas.minghua.contract.SupplementaryFactory;
import com.kingdee.eas.minghua.contract.SupplementaryInfo;
import com.kingdee.eas.minghua.contract.UPOthercontractFactory;
import com.kingdee.eas.minghua.contract.UPOthercontractInfo;
import com.kingdee.eas.minghua.contract.XzqtdocFactory;
import com.kingdee.eas.minghua.contract.XzqtdocInfo;
import com.kingdee.eas.minghua.contract.ZyfbhtDocFactory;
import com.kingdee.eas.minghua.contract.ZyfbhtDocInfo;
import com.kingdee.eas.minghua.jczlwh.mj.SfshEnum;
import com.kingdee.eas.minghua.material.Jzygclcght2DocFactory;
import com.kingdee.eas.minghua.material.Jzygclcght2DocInfo;

public class SupplementaryControllerBean extends AbstractSupplementaryControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.minghua.contract.app.SupplementaryControllerBean");
    
	protected boolean _audit(Context ctx, String audit) throws BOSException {
		try {
			SupplementaryInfo Clc =  SupplementaryFactory.getLocalInstance(ctx).getSupplementaryInfo(new ObjectUuidPK(audit));
			if(Clc.getDjstage().equals(SfshEnum.shenhez)){
			Clc.setAuditor(SysContext.getSysContext().getCurrentUserInfo());
			Clc.setDjstage(SfshEnum.yes);
		   SupplementaryFactory.getLocalInstance(ctx).update(new ObjectUuidPK(Clc.getId().toString()), Clc);
		    String zcHtbID =  Clc.getSourceBillId();
		    if(Clc.isUpdateAmount()==true){
		    	 IObjectValue billInfos = (IObjectValue) DynamicObjectFactory.getLocalInstance(ctx).getValue(new ObjectUuidPK(zcHtbID).getObjectType(),new ObjectUuidPK(zcHtbID));
					if(billInfos instanceof BcxyDocInfo){
						BcxyDocInfo bcxyInfo = (BcxyDocInfo)billInfos;
						if(bcxyInfo.getSgzj()==null){
							bcxyInfo.setSgzj(Clc.getAgreementAmount());
						}else{
							bcxyInfo.setSgzj(bcxyInfo.getSgzj().add(Clc.getAgreementAmount()));
						}
						BcxyDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					   } 
					if(billInfos instanceof ZyfbhtDocInfo){
						 ZyfbhtDocInfo bcxyInfo = (ZyfbhtDocInfo)billInfos;
						 if(bcxyInfo.getHshtje()==null){
							 bcxyInfo.setHshtje(Clc.getAgreementAmount()); 
						 }else{
							 bcxyInfo.setHshtje(bcxyInfo.getHshtje().add(Clc.getAgreementAmount())); 
						 }
						 ZyfbhtDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof LwfbhtDocInfo){
						LwfbhtDocInfo bcxyInfo = (LwfbhtDocInfo)billInfos;
						 if(bcxyInfo.getHtj()==null){
							 bcxyInfo.setHtj(Clc.getAgreementAmount()); 
						 }else{
							 bcxyInfo.setHtj(bcxyInfo.getHtj().add(Clc.getAgreementAmount())); 
						 }
						 LwfbhtDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof Jzygclcght2DocInfo){
						Jzygclcght2DocInfo bcxyInfo = (Jzygclcght2DocInfo)billInfos;
						if(bcxyInfo.getHtje()==null){
							 bcxyInfo.setHtje(Clc.getAgreementAmount()); 
						 }else{
							 bcxyInfo.setHtje(bcxyInfo.getHtje().add(Clc.getAgreementAmount())); 
						 }
						 Jzygclcght2DocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof UPOthercontractInfo){
						UPOthercontractInfo bcxyInfo = (UPOthercontractInfo)billInfos;
						if(bcxyInfo.getHtzj()==null){
							 bcxyInfo.setHtzj(Clc.getAgreementAmount()); 
						 }else{
							 bcxyInfo.setHtzj(bcxyInfo.getHtzj().add(Clc.getAgreementAmount())); 
						 }
						 UPOthercontractFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof XzqtdocInfo){
						XzqtdocInfo bcxyInfo = (XzqtdocInfo)billInfos;
						if(bcxyInfo.getHtAmount()==null){
							 bcxyInfo.setHtAmount(Clc.getAgreementAmount()); 
						 }else{
							 bcxyInfo.setHtAmount(bcxyInfo.getHtAmount().add(Clc.getAgreementAmount())); 
						 }
						 XzqtdocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof QthtDocInfo){
						QthtDocInfo bcxyInfo = (QthtDocInfo)billInfos;
						if(bcxyInfo.getHtzj()==null){
							 bcxyInfo.setHtzj(Clc.getAgreementAmount()); 
						 }else{
							 bcxyInfo.setHtzj(bcxyInfo.getHtzj().add(Clc.getAgreementAmount())); 
						 }
						QthtDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
		       }
			}
			
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		return super._audit(ctx, audit);
	}

	protected boolean _unaudit(Context ctx, String unaudit) throws BOSException {
		try {
			SupplementaryInfo TO=SupplementaryFactory.getLocalInstance(ctx).getSupplementaryInfo(new ObjectUuidPK(unaudit));
		    if(TO.getDjstage().equals(SfshEnum.yes)){
			TO.setDjstage(SfshEnum.save);
		    SupplementaryFactory.getLocalInstance(ctx).update(new ObjectUuidPK(TO.getId().toString()), TO);
		    String zcHtbID =  TO.getSourceBillId();
		    if(TO.isUpdateAmount()==true){
		    	 IObjectValue billInfos = (IObjectValue) DynamicObjectFactory.getLocalInstance(ctx).getValue(new ObjectUuidPK(zcHtbID).getObjectType(),new ObjectUuidPK(zcHtbID));
					if(billInfos instanceof BcxyDocInfo){
						BcxyDocInfo bcxyInfo = (BcxyDocInfo)billInfos;
						bcxyInfo.setSgzj(bcxyInfo.getSgzj().subtract(TO.getAgreementAmount()));
						BcxyDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					   } 
					if(billInfos instanceof ZyfbhtDocInfo){
						 ZyfbhtDocInfo bcxyInfo = (ZyfbhtDocInfo)billInfos;
						 bcxyInfo.setHshtje(bcxyInfo.getHshtje().subtract(TO.getAgreementAmount()));
						 ZyfbhtDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof LwfbhtDocInfo){
						LwfbhtDocInfo bcxyInfo = (LwfbhtDocInfo)billInfos;
						 bcxyInfo.setHtj(bcxyInfo.getHtj().subtract(TO.getAgreementAmount()));
						 LwfbhtDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof Jzygclcght2DocInfo){
						Jzygclcght2DocInfo bcxyInfo = (Jzygclcght2DocInfo)billInfos;
						 bcxyInfo.setHtje(bcxyInfo.getHtje().subtract(TO.getAgreementAmount()));
						 Jzygclcght2DocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof UPOthercontractInfo){
						UPOthercontractInfo bcxyInfo = (UPOthercontractInfo)billInfos;
						 bcxyInfo.setHtzj(bcxyInfo.getHtzj().subtract(TO.getAgreementAmount()));
						 UPOthercontractFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof XzqtdocInfo){
						XzqtdocInfo bcxyInfo = (XzqtdocInfo)billInfos;
						 bcxyInfo.setHtAmount(bcxyInfo.getHtAmount().subtract(TO.getAgreementAmount()));
						 XzqtdocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
					if(billInfos instanceof QthtDocInfo){
						QthtDocInfo bcxyInfo = (QthtDocInfo)billInfos;
						 bcxyInfo.setHtzj(bcxyInfo.getHtzj().subtract(TO.getAgreementAmount()));
						 XzqtdocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(zcHtbID), bcxyInfo);
					 }
		       }
		    }
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		   return super._unaudit(ctx, unaudit);
	
	}
}